package com.godenwater.recv.server.qx;


import com.godenwater.recv.model.CommonMessage;
import com.godenwater.recv.server.yf.YfMessageHandler;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 报文接收处理器，为加快报文的处理，对接收后的报文仅保存为文件，以提高接收的效率
 *
 * @author 李普军
 * @date 2016-10-18
 */
public class WeaterServerDataHandler extends IoFilterAdapter implements IoHandler {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final WeaterServer server;

    private String channel;

    private int recvPort;

    public WeaterServerDataHandler(WeaterServer server, String channel, int recvPort) {
        this.server = server;
        this.channel = channel;
        this.recvPort = recvPort;
    }

    /**
     * 消息接收后的处理，只处理业务逻辑，此消息处理是在解码后，所以只需要关注消息体的内容
     */
    public void messageReceived(IoSession session, Object message)
            throws Exception {

        logger.debug("接收通道" + this.channel + " 服务端已收到 " + server.messageSize()
                + " 消息数;");


        if (message instanceof CommonMessage) {
            // 纯粹只处理报文，此消息体已经被解码为单体报文
            CommonMessage msg = (CommonMessage) message;
            WeaterMessageHandler.getInstance().perform(channel, session, msg, recvPort);

        }

    }

    /**
     * {@inheritDoc}
     */
    public void sessionClosed(IoSession session) throws Exception {
        logger.debug("关闭会话  " + session.getRemoteAddress());
        //RtuServer.getInstance().getSessionManager().removeSession(session);
    }

    /**
     * 当session创建连接后，需添加到一个客户端中
     */
    public void sessionCreated(IoSession session) throws Exception {
//		logger.debug("----------------------------------------------------------------------");
//		logger.debug("创建会话  " + session.getRemoteAddress());
        //RtuServer.getInstance().getSessionManager().createSession(session);
    }

    /**
     * {@inheritDoc} 在此对空闲的session进行关闭
     */
    public void sessionIdle(IoSession session, IdleStatus status)
            throws Exception {
//		logger.debug("空闲会话  " + session.getRemoteAddress());
        //RtuServer.getInstance().getSessionManager().idleSession(session);
    }

    /**
     * {@inheritDoc}
     */
    public void sessionOpened(IoSession session) throws Exception {
        // logger.info("Session Opened...");

    }

    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        logger.debug("会话出现异常  " + session.getRemoteAddress() + " \t"
                + cause.getMessage());
        //RtuServer.getInstance().getSessionManager().removeSession(session);
        session.close(true);

    }

    /**
     * 消息应答事件,表示发送消息成功后，调用这个方法
     */
    public void messageSent(IoSession session, Object message) throws Exception {
        logger.debug("报文下发完成！  ");
        // session.close(true);
    }


    public void inputClosed(IoSession arg0) throws Exception {
        //logger.info("inputClosed 出现异常！151行  " );
    }
}
